package com.eveinfo.display.planner;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.Window;
import android.widget.ListView;
import android.widget.TextView;

import com.eveinfo.R;
import com.eveinfo.display.SheetView;
import com.eveinfo.tools.ActivityLauncher;
import com.eveinfo.tools.EveInfoMenu;
import com.eveinfo.tools.Tools;

public class PlannerView extends Activity {

	private Planner planner;
	private TextView textView;
	private PlannerAdapter plannerAdapter;
	private int characterID;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.planner);
		
		characterID = getIntent().getIntExtra("characterID", 0);

		String plannerName = getIntent().getStringExtra("plannerName");
		planner = SheetView.getPlannerManager().getPlanner(plannerName);

		textView = (TextView) findViewById(R.id.TVPlanner);
		ListView listView = (ListView) findViewById(R.id.LVPlanner);

		if (planner == null) {
			finish();
		} else {
			setHeader();
			plannerAdapter = new PlannerAdapter(this, listView, planner, characterID);
		}
	}

	@Override
	public void onContentChanged() {
		super.onContentChanged();
		System.out.println("onContentChanged");
	}

	@Override
	public void onUserInteraction() {
		setHeader();
		System.out.println("onUserInteraction");
		super.onUserInteraction();
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		System.out.println("onTouchEvent");
		return super.onTouchEvent(event);
	}

	@Override
	protected void onResume() {
		super.onResume();
		setHeader();
		plannerAdapter.notifyDataSetChanged();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(0, EveInfoMenu.MENU_CHAR_SKILLS, 0, EveInfoMenu.MENU_CHAR_SKILLS_NAME)
				.setIcon(R.drawable.icon22_41_32);
		menu.add(0, EveInfoMenu.MENU_CERTIFICATES, 0, EveInfoMenu.MENU_CERTIFICATES_NAME).setIcon(
				R.drawable.icon79_01_32);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case EveInfoMenu.MENU_CHAR_SKILLS:
			ActivityLauncher.displaySkills(this, characterID);
			return true;
		case EveInfoMenu.MENU_CERTIFICATES:
			ActivityLauncher.displayCategories(this, characterID);
			return true;
		default:
			return false;
		}
	}

	private void setHeader() {
		if (textView == null)
			return;
		textView.setText(planner.getName() + "\n" + planner.getSize() + " Skills planned.\n" + "Training time: "
				+ Tools.convertMinutesToTime(planner.getTime()));
	}
}
